package org.sump.mysql;

import java.util.Arrays;
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;

import org.sump.data.Permission;
import org.sump.data.ResourceType;

/**
 * Declares MySQL resource types ("table", "database" etc). 
 * @author Sergey Grigorchuk (sergey.grigorchuk@gmail.com)
 */
public enum MySQLResourceType implements ResourceType {
	DATABASE(new Permission[] {
			MySQLPermission.SELECT,
			MySQLPermission.INSERT,
			MySQLPermission.UPDATE,
			MySQLPermission.DELETE,
			MySQLPermission.CREATE,
			MySQLPermission.DROP,
			MySQLPermission.GRANT,
			MySQLPermission.REFERENCES,
			MySQLPermission.INDEX,
			MySQLPermission.ALTER,
			MySQLPermission.CREATE_TMP_TABLE,
			MySQLPermission.LOCK_TABLES,
			MySQLPermission.CREATE_VIEW,
			MySQLPermission.SHOW_VIEW,
			MySQLPermission.CREATE_ROUTINE,
			MySQLPermission.ALTER_ROUTINE,
			MySQLPermission.EXECUTE,
			MySQLPermission.EVENT,
			MySQLPermission.TRIGGER,
	}),
	TABLE(new Permission[] {
			MySQLPermission.SELECT,
			MySQLPermission.INSERT,
			MySQLPermission.UPDATE,
			MySQLPermission.DELETE,
			MySQLPermission.CREATE,
			MySQLPermission.DROP,
			MySQLPermission.GRANT,
			MySQLPermission.REFERENCES,
			MySQLPermission.INDEX,
			MySQLPermission.ALTER,
			MySQLPermission.CREATE_VIEW,
			MySQLPermission.SHOW_VIEW,
			MySQLPermission.TRIGGER,
	}),
	VIEW(new Permission[] {
			MySQLPermission.SELECT,
			MySQLPermission.INSERT,
			MySQLPermission.UPDATE,
			MySQLPermission.DELETE,
			MySQLPermission.CREATE,
			MySQLPermission.DROP,
			MySQLPermission.GRANT,
			MySQLPermission.REFERENCES,
			MySQLPermission.INDEX,
			MySQLPermission.ALTER,
			MySQLPermission.CREATE_VIEW,
			MySQLPermission.SHOW_VIEW,
			MySQLPermission.TRIGGER,
	}),
	PROCEDURE(new Permission[] {
			MySQLPermission.EXECUTE,
			MySQLPermission.ALTER_ROUTINE,
			MySQLPermission.GRANT,
	}),
	FUNCTION(new Permission[] {
			MySQLPermission.EXECUTE,
			MySQLPermission.ALTER_ROUTINE,
			MySQLPermission.GRANT,
	}),
	COLUMN(new Permission[] {
			MySQLPermission.SELECT,
			MySQLPermission.INSERT,
			MySQLPermission.UPDATE,
			MySQLPermission.REFERENCES,
	}),
	SERVER(new Permission[] {
			MySQLPermission.SELECT,
			MySQLPermission.INSERT,
			MySQLPermission.UPDATE,
			MySQLPermission.DELETE,
			MySQLPermission.CREATE,
			MySQLPermission.DROP,
			MySQLPermission.RELOAD,
			MySQLPermission.SHUTDOWN,
			MySQLPermission.PROCESS,
			MySQLPermission.FILE,
			MySQLPermission.GRANT,
			MySQLPermission.REFERENCES,
			MySQLPermission.INDEX,
			MySQLPermission.ALTER,
			MySQLPermission.SHOW_DB,
			MySQLPermission.SUPER,
			MySQLPermission.CREATE_TMP_TABLE,
			MySQLPermission.LOCK_TABLES,
			MySQLPermission.EXECUTE,
			MySQLPermission.REPL_SLAVE,
			MySQLPermission.REPL_CLIENT,
			MySQLPermission.CREATE_VIEW,
			MySQLPermission.SHOW_VIEW,
			MySQLPermission.CREATE_ROUTINE,
			MySQLPermission.ALTER_ROUTINE,
			MySQLPermission.CREATE_USER,
			MySQLPermission.EVENT,
			MySQLPermission.TRIGGER,
	}),
	HOST(new Permission[] {
			MySQLPermission.SELECT,
			MySQLPermission.INSERT,
			MySQLPermission.UPDATE,
			MySQLPermission.DELETE,
			MySQLPermission.CREATE,
			MySQLPermission.DROP,
			MySQLPermission.GRANT,
			MySQLPermission.REFERENCES,
			MySQLPermission.INDEX,
			MySQLPermission.ALTER,
			MySQLPermission.CREATE_TMP_TABLE,
			MySQLPermission.LOCK_TABLES,
			MySQLPermission.CREATE_VIEW,
			MySQLPermission.SHOW_VIEW,
			MySQLPermission.CREATE_ROUTINE,
			MySQLPermission.ALTER_ROUTINE,
			MySQLPermission.EXECUTE,
			MySQLPermission.TRIGGER,
	}),
	;
	
	private final Set<Permission> permissions;
	
	private MySQLResourceType(Permission[] permissions) {
		this.permissions = Collections.unmodifiableSortedSet(new TreeSet<Permission>(Arrays.asList(permissions)));
	}

	public Set<Permission> getPermissions() {
		return permissions;
	}

	public String getName() {
		return name();
	}
}
